package com.meishij.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jash.myutils.utils.ImageUtils;
import com.meishij.R;
import com.meishij.entry.RecommendEntry;
import com.meishij.fragment.RecommendCateresFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/13.
 * <p>
 * ,.
 * ,_> `.   ,';
 * ,-`'      `'   '`'._
 * ,,-) ---._   |   .---''`-),.
 * ,'      `.  \  ;  /   _,'     `,
 * ,--' ____       \   '  ,'    ___  `-,
 * _>   /--. `-.              .-'.--\   \__
 * '-,  (    `.  `.,`~ \~'-. ,' ,'    )    _\
 * _<    \     \ ,'  ') )   `. /     /    <,.
 * ,-'   _,  \    ,'    ( /      `.    /        `-,
 * `-.,-'     `.,'       `         `.,'  `\    ,-'
 * ,'       _  /   ,,,      ,,,     \     `-. `-._
 * /-,     ,'  ;   ' _ \    / _ `     ; `.     `(`-\
 * /-,        ;    (o)      (o)      ;          `'`,
 * ,~-'  ,-'    \     '        `      /     \      <_
 * /-. ,'        \                   /       \     ,-'
 * '`,     ,'   `-/             \-' `.      `-. <
 * /_    /      /   (_     _)   \    \          `,
 * `-._;  ,' |  .::.`-.-' :..  |       `-.    _\
 * _/       \  `:: ,^. :.:' / `.        \,-'
 * '`.   ,-'  /`-..-'-.-`-..-'\            `-.
 * >_ /     ;  (\/( ' )\/)  ;     `-.    _<
 * ,-'      `.  \`-^^^-'/  ,'        \ _<
 * `-,  ,'   `. `"""""' ,'   `-.   <`'
 * ')        `._.,,_.'        \ ,-'
 * '._        '`'`'   \       <
 * >   ,'       ,   `-.   <`'
 * `,/          \      ,-`
 * `,   ,' |   /     /
 * '; /   ;        (
 * _)|   `       (
 * `')         .-'
 * <_   \   /    hjw
 * \   /\(
 * `;/  `
 */

public class RecommendRecyclerViewAdapter extends RecyclerView.Adapter<RecommendRecyclerViewAdapter.ViewHolder> implements ViewPager.OnPageChangeListener {

    private RecommendEntry.ObjBean obj;
    private Context context;
    private FragmentManager childFragmentManager;
    private ViewHolder viewHolder;

    public RecommendRecyclerViewAdapter(FragmentManager childFragmentManager, RecommendEntry.ObjBean obj) {
        this.obj = obj;
        this.childFragmentManager = childFragmentManager;
    }


    @Override
    public int getItemViewType(int position) {
        return position;//根据位置加载不同的布局
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = null;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.fragment_recommend_body_cateres, parent, false);
                break;
            case 1:
                view = inflater.inflate(R.layout.fragment_recommend_body_classify, parent, false);
                break;
            case 2:
                view = inflater.inflate(R.layout.fragment_recommend_body_func, parent, false);
                break;
            case 3:
                view = inflater.inflate(R.layout.fragment_recommend_body_top3, parent, false);
                break;
            case 4:
                view = inflater.inflate(R.layout.fragment_recommend_body_shops, parent, false);
                break;
            case 5:
                view = inflater.inflate(R.layout.fragment_recommend_body_zt, parent, false);
                break;
        }

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder.recommend_body_viewPager != null) {
//            viewHolder = new ViewHolder(new View(context));
            viewHolder = holder;
            List<RecommendEntry.ObjBean.SanCanBean> sanCanBeanList = new ArrayList<>();
            List<Fragment> fragmentList = new ArrayList<>();
            int anInt = 0;
            for (int i = 0; i < obj.getSan_can().size(); i++) {
                sanCanBeanList.add(obj.getSan_can().get(i));
                if ((i + 1) % 3 == 0) {
                    RecommendCateresFragment instance = RecommendCateresFragment.getInstance();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("sanCanBean1", sanCanBeanList.get(0));
                    bundle.putSerializable("sanCanBean2", sanCanBeanList.get(1));
                    bundle.putSerializable("sanCanBean3", sanCanBeanList.get(2));
                    bundle.putSerializable("sanCanTitlesBean", obj.getSan_can_titles().get(anInt));
                    anInt++;
                    instance.setArguments(bundle);
                    fragmentList.add(instance);
                    sanCanBeanList.removeAll(sanCanBeanList);
                }
            }
            RecommendCateresFragmentAdapter recommendCateresFragmentAdapter = new RecommendCateresFragmentAdapter(childFragmentManager, fragmentList);
            holder.recommend_body_viewPager.setAdapter(recommendCateresFragmentAdapter);
            holder.recommend_body_viewPager.addOnPageChangeListener(this);
            for (int i = 0; i < fragmentList.size(); i++) {
                ImageView imageView = new ImageView(context);
                imageView.setPadding(10, 10, 10, 10);
                imageView.setImageResource(R.drawable.recommend_white_point);
                if (holder.recommend_body_linearLayout.getChildCount() <fragmentList.size())
                    holder.recommend_body_linearLayout.addView(imageView);
            }
        }

        if (holder.recommend_body_classify_item_image_one != null) {
            List<RecommendEntry.ObjBean.FenleiBean> fenlei = obj.getFenlei();
            holder.recommend_body_classify_item_image_one.setImageURI(Uri.parse(fenlei.get(0).getImage()));
            holder.recommend_body_classify_item_title_one.setText(fenlei.get(0).getTitle());
            holder.recommend_body_classify_item_image_two.setImageURI(Uri.parse(fenlei.get(1).getImage()));
            holder.recommend_body_classify_item_title_two.setText(fenlei.get(1).getTitle());
            holder.recommend_body_classify_item_image_three.setImageURI(Uri.parse(fenlei.get(2).getImage()));
            holder.recommend_body_classify_item_title_three.setText(fenlei.get(2).getTitle());
            holder.recommend_body_classify_item_image_four.setImageURI(Uri.parse(fenlei.get(3).getImage()));
            holder.recommend_body_classify_item_title_four.setText(fenlei.get(3).getTitle());
        }

        if (holder.recommend_body_func_image_one != null) {
            ImageUtils.loadImage(holder.recommend_body_func_image_one, obj.getFunc1().getImage(), R.mipmap.ic_launcher);
            ImageUtils.loadImage(holder.recommend_body_func_image_two, obj.getFunc2().getImage(), R.mipmap.ic_launcher);
        }

        if (holder.recommend_body_top3_viewpager != null) {
            List<RecommendEntry.ObjBean.Top3Bean> top3 = obj.getTop3();
            holder.recommend_body_top3_viewpager.setAdapter(new RecommendTopViewPagerAdapter(top3));
            holder.recommend_body_top3_viewpager.addOnPageChangeListener(holder);
            holder.handler.obtainMessage(0, 0, 0).sendToTarget();
            for (int i = 0; i < top3.size(); i++) {
                ImageView imageView = new ImageView(context);
                imageView.setImageResource(R.drawable.recommend_white_point);
                imageView.setPadding(10, 10, 10, 10);
                holder.recommend_body_top3_linearLayout.addView(imageView);
            }
        }


        if (holder.recommend_body_shops_gridView != null) {
            List<RecommendEntry.ObjBean.ShopsBean> shops = obj.getShops();
            holder.recommend_body_shops_gridView.setAdapter(new RecommendGridViewAdaptr(shops));
        }

        if (holder.recommend_body_zt_image_one != null) {
            ImageUtils.loadImage(holder.recommend_body_zt_image_one, obj.getZt().get(0).getPhoto(), R.mipmap.ic_launcher);
            ImageUtils.loadImage(holder.recommend_body_zt_image_two, obj.getZt().get(1).getPhoto(), R.mipmap.ic_launcher);
            ImageUtils.loadImage(holder.recommend_body_zt_image_three, obj.getZt().get(2).getPhoto(), R.mipmap.ic_launcher);
            ImageUtils.loadImage(holder.recommend_body_zt_image_four, obj.getZt().get(3).getPhoto(), R.mipmap.ic_launcher);
        }


    }

    @Override
    public int getItemCount() {
        if (obj != null)
            return 6;
        return 0;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (viewHolder.recommend_body_linearLayout != null) {
            viewHolder.recommend_body_image.setVisibility(View.VISIBLE);
            float value = (viewHolder.recommend_body_linearLayout.getLeft() + (position + positionOffset) * viewHolder.recommend_body_image.getWidth());
            ViewCompat.setTranslationX(viewHolder.recommend_body_image, value);
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    public static class ViewHolder extends RecyclerView.ViewHolder implements ViewPager.OnPageChangeListener, Handler.Callback {

        private final LinearLayout recommend_body_top3_linearLayout;
        private final ImageView recommend_body_top3_image;
        private final LinearLayout recommend_body_linearLayout;
        private final ImageView recommend_body_image;
        private Handler handler = new Handler(this);

        private ViewPager recommend_body_viewPager;

        private final View recommend_body_classify_one;
        private SimpleDraweeView recommend_body_classify_item_image_one = null;
        private TextView recommend_body_classify_item_title_one = null;
        private final View recommend_body_classify_two;
        private SimpleDraweeView recommend_body_classify_item_image_two = null;
        private TextView recommend_body_classify_item_title_two = null;
        private final View recommend_body_classify_three;
        private SimpleDraweeView recommend_body_classify_item_image_three = null;
        private TextView recommend_body_classify_item_title_three = null;
        private final View recommend_body_classify_four;
        private SimpleDraweeView recommend_body_classify_item_image_four = null;
        private TextView recommend_body_classify_item_title_four = null;

        private final ImageView recommend_body_func_image_one;
        private final ImageView recommend_body_func_image_two;

        private final ViewPager recommend_body_top3_viewpager;

        private final GridView recommend_body_shops_gridView;

        private final ImageView recommend_body_zt_image_one;
        private final ImageView recommend_body_zt_image_two;
        private final ImageView recommend_body_zt_image_three;
        private final ImageView recommend_body_zt_image_four;

        public ViewHolder(View itemView) {
            super(itemView);
            recommend_body_viewPager = ((ViewPager) itemView.findViewById(R.id.recommend_body_viewPager));
            recommend_body_linearLayout = ((LinearLayout) itemView.findViewById(R.id.recommend_body_linearLayout));
            recommend_body_image = ((ImageView) itemView.findViewById(R.id.recommend_body_image));

            recommend_body_classify_one = itemView.findViewById(R.id.recommend_body_classify_one);
            if (recommend_body_classify_one != null) {
                recommend_body_classify_item_image_one = ((SimpleDraweeView) recommend_body_classify_one.findViewById(R.id.recommend_body_classify_item_image));
                recommend_body_classify_item_title_one = ((TextView) recommend_body_classify_one.findViewById(R.id.recommend_body_classify_item_title));
            }
            recommend_body_classify_two = itemView.findViewById(R.id.recommend_body_classify_two);
            if (recommend_body_classify_two != null) {
                recommend_body_classify_item_image_two = ((SimpleDraweeView) recommend_body_classify_two.findViewById(R.id.recommend_body_classify_item_image));
                recommend_body_classify_item_title_two = ((TextView) recommend_body_classify_two.findViewById(R.id.recommend_body_classify_item_title));
            }
            recommend_body_classify_three = itemView.findViewById(R.id.recommend_body_classify_three);
            if (recommend_body_classify_three != null) {
                recommend_body_classify_item_image_three = ((SimpleDraweeView) recommend_body_classify_three.findViewById(R.id.recommend_body_classify_item_image));
                recommend_body_classify_item_title_three = ((TextView) recommend_body_classify_three.findViewById(R.id.recommend_body_classify_item_title));
            }
            recommend_body_classify_four = itemView.findViewById(R.id.recommend_body_classify_four);
            if (recommend_body_classify_four != null) {
                recommend_body_classify_item_image_four = ((SimpleDraweeView) recommend_body_classify_four.findViewById(R.id.recommend_body_classify_item_image));
                recommend_body_classify_item_title_four = ((TextView) recommend_body_classify_four.findViewById(R.id.recommend_body_classify_item_title));
            }

            recommend_body_func_image_one = ((ImageView) itemView.findViewById(R.id.recommend_body_func_image_one));
            recommend_body_func_image_two = ((ImageView) itemView.findViewById(R.id.recommend_body_func_image_two));

            recommend_body_top3_viewpager = ((ViewPager) itemView.findViewById(R.id.recommend_body_top3_viewpager));
            recommend_body_top3_linearLayout = ((LinearLayout) itemView.findViewById(R.id.recommend_body_top3_linearLayout));
            recommend_body_top3_image = ((ImageView) itemView.findViewById(R.id.recommend_body_top3_image));

            recommend_body_shops_gridView = ((GridView) itemView.findViewById(R.id.recommend_body_shops_recyclerView));

            recommend_body_zt_image_one = ((ImageView) itemView.findViewById(R.id.recommend_body_zt_image_one));
            recommend_body_zt_image_two = ((ImageView) itemView.findViewById(R.id.recommend_body_zt_image_two));
            recommend_body_zt_image_three = ((ImageView) itemView.findViewById(R.id.recommend_body_zt_image_three));
            recommend_body_zt_image_four = ((ImageView) itemView.findViewById(R.id.recommend_body_zt_image_four));
        }


        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            recommend_body_top3_image.setVisibility(View.VISIBLE);
            float value = recommend_body_top3_linearLayout.getLeft() + (position - 1 + positionOffset) * recommend_body_top3_image.getWidth();
            ViewCompat.setTranslationX(recommend_body_top3_image, value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter adapter = recommend_body_top3_viewpager.getAdapter();
            if (position == adapter.getCount() - 1) {
                recommend_body_top3_viewpager.setCurrentItem(1, false);
            } else if (position == 0) {
                recommend_body_top3_viewpager.setCurrentItem(adapter.getCount() - 2, false);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }


        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ViewPager pager = this.recommend_body_top3_viewpager;
                    if (msg.arg1 == pager.getAdapter().getCount() - 1) {
                        msg.arg1 = 1;
                    }
                    pager.setCurrentItem(msg.arg1, msg.arg1 != 1);
                    Message message = new Message();
                    message.what = 0;
                    message.arg1 = ++(msg.arg1);
                    handler.sendMessageDelayed(message, 2000);
                    break;
            }
            return false;
        }
    }

}
